package com.ck.ips.service.impl;

import com.ck.common.model.OperationResult;
import com.ck.ips.dao.DataBoardMapper;
import com.ck.ips.model.vo.board.*;
import com.ck.ips.service.DataBoardService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

/**
 * 数据看板服务实现类
 *
 * @author XC17125
 */
@Service(value = "dataBoardService")
public class DataBoardServiceImpl implements DataBoardService {

    @Autowired
    private DataBoardMapper dataBoardMapper;

    @Override
    public OperationResult getRealTimeProgram() {
        List<ProgramRealTimeVo> daoRes = dataBoardMapper.getRealTimeProgram();
        return OperationResult.newSuccessOperationResult("获取成功", daoRes);
    }

    @Override
    public OperationResult getMainStatic() {
        MainStaticVo mainStaticVo = dataBoardMapper.getStoreStaticInfo();
        if (mainStaticVo == null) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        mainStaticVo.setDeviceCount(dataBoardMapper.getDeviceCount());
        return OperationResult.newSuccessOperationResult("获取成功", mainStaticVo);
    }

    @Override
    public OperationResult getProvinceMapData() {
        List<ProvinceMapVo> daoRes = dataBoardMapper.getProvinceMapData();
        if (CollectionUtils.isEmpty(daoRes)) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        return OperationResult.newSuccessOperationResult(daoRes);
    }

    @Override
    public OperationResult getProgramCountByMonth() {
        List<ProgramCountVo> daoRes = dataBoardMapper.getProgramCountByMonth();
        if (CollectionUtils.isEmpty(daoRes)) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        return OperationResult.newSuccessOperationResult(daoRes);
    }

    @Override
    public OperationResult getScreenSizeCount() {
        List<ScreenCountVo> daoRes = dataBoardMapper.getScreenSizeCount();
        if (CollectionUtils.isEmpty(daoRes)) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        return OperationResult.newSuccessOperationResult(daoRes);
    }

    @Override
    public OperationResult getStoreProgramCount() {
        Long startTime = getYearStartTime();
        List<StoreProgramVo> daoRes = dataBoardMapper.getStoreProgramCount(startTime);
        if (CollectionUtils.isEmpty(daoRes)) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        return OperationResult.newSuccessOperationResult(daoRes);
    }

    @Override
    public OperationResult getLedStatus() {
        List<LedStatisticVo> daoRes = dataBoardMapper.getLedStatus();
        if (CollectionUtils.isEmpty(daoRes)) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        return OperationResult.newSuccessOperationResult(daoRes);
    }

    @Override
    public OperationResult getMonitorData() {
        List<MonitorDataVo> daoRes = dataBoardMapper.getMonitorData();
        if (CollectionUtils.isEmpty(daoRes)) {
            return OperationResult.newFailOperationResult("未查询到相关数据");
        }
        return OperationResult.newSuccessOperationResult(daoRes);
    }

    /**
     * 获取当年的开始时间戳
     *
     * @return 本年度第一天的时间戳
     */
    private Long getYearStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }


}
